//
//  MainViewController.swift
//  JMWB
//
//  Created by 个人电脑 on 16/3/13.
//  Copyright © 2016年 个人电脑. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {
//关键字private 表示私有的,只能在当前文件中使用
//关键字lazy 表示懒加载,使用时才加载
    private lazy var imageNames :[String] = ["tabbar_home", "tabbar_message_center", "","tabbar_discover", "tabbar_profile"]
 //通过类方法
//    private lazy var composeBtn : UIButton = UIButton.createBtn("tabbar_compose_icon_add", bgImageName: "tabbar_compose_button")
  //通过便利构造函数方法
     private lazy var composeBtn : UIButton = UIButton(imageName: "tabbar_compose_icon_add", bgImageName: "tabbar_compose_button")
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupComposeBtn()
       }
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        adjustItem()
    }
}
//关键字extension 表示类扩展,相当于OC中的方法
extension MainViewController{
    private func adjustItem() {
    for i in 0..<tabBar.items!.count{
        let item = tabBar.items![i]
        if i == 2{
        item.enabled = false
            continue
        }
        item.selectedImage = UIImage(named: imageNames[i] + "_highlighted")
        }
    }
    
    private func setupComposeBtn(){
    
        tabBar.addSubview(composeBtn)
//        composeBtn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: .Normal)
//        composeBtn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: .Highlighted)
//        composeBtn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: .Normal)
//        composeBtn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: .Highlighted)
//        
//        composeBtn.sizeToFit()
        composeBtn.center = CGPoint(x: tabBar.bounds.width * 0.5, y: tabBar.bounds.height * 0.5)
        
      // Selector包装:1.Selector("composeBtnClick") 2."composeBtnClick"
        composeBtn.addTarget(self, action: "composeBtnClick", forControlEvents: .TouchUpInside)
        
    }
}

// OC中监听事件本质:给某一个对象发送消息. 发送消息的本质:去对象消息列表中寻找SEL
// 在swift中,如果将一个函数声明称private,那么该函数不会被添加到消息列表中
// 如果在私有函数前加@objc,那么该函数依然是私有,并且会被添加到消息列表中

extension MainViewController{
    @objc private func composeBtnClick(){
       let composeVc = ComposeViewController()
        
       let composeNav = UINavigationController(rootViewController: composeVc)
        
       presentViewController(composeNav, animated: true, completion: nil)
    }
}
    


